package com.ais.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ais.service.DeptmentManage;
import com.ais.service.PersonManage;
import com.ais.vo.Deptment;
import com.ais.vo.Person;
import com.ais.web.dto.ResponseBean;
import com.ais.web.dto.ResponseBeanList;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import common.Constant;
import exception.LogicException;

@Controller
@RequestMapping("/Person")
public class PersonController {
	@Autowired
	private PersonManage personManage;
	@Autowired
	private DeptmentManage deptmentManage;
	
	@RequestMapping("/toPerson.do")
	public ModelAndView toPerson(){
		return new ModelAndView("addPerson");
	}
	//用户注册
	@ResponseBody()
	@RequestMapping("/registerPerson.do")
	public String registerPerson(HttpServletRequest request,
			HttpServletResponse response,Person person){
		ResponseBean bean = new ResponseBean();
		try{
			this.personManage.addPerson(person);
			bean.setCode(Constant.REQ_SUCCESS);
		}catch (Exception e) {
			bean.setCode(Constant.REQ_ERROR);
			if(e instanceof LogicException){
				bean.setDescription(((LogicException)e).getMsg());
			}
		}
		Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();  
	    String json = gson.toJson(bean); 
		return json;
	}
	//部门列表
	@ResponseBody()
	@RequestMapping("/getDepList.do")
	public String getDepList(HttpServletRequest request,
			HttpServletResponse response){
		ResponseBeanList<Deptment> bean = new ResponseBeanList<Deptment>();
		try{
			List<Deptment> depList = this.deptmentManage.getDeptmentList();
			bean.setCode(Constant.REQ_SUCCESS);
			bean.setList(depList);
		}catch (Exception e) {
			bean.setCode(Constant.REQ_ERROR);
			if(e instanceof LogicException){
				bean.setDescription(((LogicException)e).getMsg());
			}
		}
		Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();  
	    String json = gson.toJson(bean); 
		return json;
	}
	@RequestMapping("/toListPerson.do")
	public String listPerson(HttpServletRequest request,
			HttpServletResponse response){
		return "listPerson";
	}
	
	@ResponseBody()
	@RequestMapping("/listPersonJson.do")
	public String listPerson(HttpServletRequest request,
			HttpServletResponse response,Person person){
		List<Person> list = this.personManage.listPerson(person);
		if(list.size()<=10){
			for(int i=0;i<10-list.size();i++){
				list.add(new Person());
			}
		}
		Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();  
	    String json = gson.toJson(list); 
		return json;
	}
	
	@ResponseBody()
	@RequestMapping("/login.do")
	public String login(HttpServletRequest request,
			HttpServletResponse response,String userName,String password){
		ResponseBean bean = new ResponseBean();
		try{
			boolean isSuccess = this.personManage.login(userName,password);
			bean.setCode(Constant.REQ_SUCCESS);
		}catch (Exception e) {
			bean.setCode(Constant.REQ_ERROR);
			if(e instanceof LogicException){
				bean.setDescription(((LogicException)e).getMsg());
			}
		}
		Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();  
	    String json = gson.toJson(bean); 
		return json;
	}

	public void setPersonManage(PersonManage personManage) {
		this.personManage = personManage;
	}
	public void setDeptmentManage(DeptmentManage deptmentManage) {
		this.deptmentManage = deptmentManage;
	}
	
}

